package com.yyfax.ph.home.getrecommendPermission;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.ph.home.GetRecommendPermissionModel;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Presenter: 获得推荐权限
 * Created by zhangxian on 2017/6/17.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class GetRecommendPermissionPresenter extends Presenter<GetRecommendPermissionModel, GetRecommendPermissionPresenter.View> {
	private static final String TAG = GetRecommendPermissionPresenter.class.getSimpleName();
	private View mView;

	public static class Generator implements PresenterFactory<GetRecommendPermissionPresenter> {
		@Override
		public GetRecommendPermissionPresenter create() {
			return new GetRecommendPermissionPresenter();
		}
	}

	public interface View extends IView {
		void invalidnumber();//10014 邀请码/公司编号无效

		void hasVerfied();//10015 推荐权限激活,用户已实名

		void notVerfied();//10016 推荐权限激活,用户未实名

		void isCompanyCode(String company);//10017 绑定母公司申请

		void bindCompanyFailure(String tips);

	}


	@Override
	public void attach(View view) {
		mView = view;
		mModel = new com.yyfax.ph.home.GetRecommendPermissionModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 绑定公司
	 */
	public void bindCompany(String code) {
		CLog.i(TAG, "bindCompany");
		execute(new com.yyfax.ph.home.GetRecommendPermissionModel.Company(code), FUNCTIONAL_CODE, true, code, null);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void companyResult(com.yyfax.ph.home.GetRecommendPermissionModel.CompanyResult result) {
		String tips = result.getMsg();
		CLog.i(TAG, "companyResult");
		if (null != result.getContent() && !TextUtils.isEmpty(result.getContent().code)) {
			if (isResponseSuccess(result)) {
				if ("10015".equals(result.getContent().code)) {  //10015 推荐权限激活,用户已实名
					mView.hasVerfied();
				} else if ("10016".equals(result.getContent().code)) {  //10016 推荐权限激活,用户未实名
					mView.notVerfied();
				}
			} else if (isResponseWarning(result)) {
				if ("10014".equals(result.getContent().code)) {  //10014 邀请码/公司编号无效
					mView.invalidnumber();
				} else if ("10017".equals(result.getContent().code)) {  //10017 绑定母公司申请
					mView.isCompanyCode(result.getContent().companyName);
				}
			}
		} else {
			mView.bindCompanyFailure(TextUtils.isEmpty(tips) ? "绑定失败" : tips);
		}
	}
}
